package com.esunny.resourcevisualization.controller;

import com.esunny.resourcevisualization.domain.entity.CustomizeColumn;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.service.CustomizeColumnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version 1.0.0.0
 * @ClassName CustomizeColumnController
 * @Description 用户自定义表头处理
 * @Author baochencong
 * @Date 2021/3/22 11:00
 */
@RequestMapping("/customizeColumn")
@Controller
public class CustomizeColumnController {

    @Resource
    private CustomizeColumnService customizeColumnService;

    @ResponseBody
    @PostMapping("/addCustomizeColumn")
    public ReturnStructure addCustomizeColumn(@RequestBody CustomizeColumn customizeColumn) {
        if (customizeColumn == null){
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("添加自定义列头信息成功",customizeColumnService.addCustomizeColumn(customizeColumn));
    }

    @ResponseBody
    @PostMapping("/deleteCustomizeColumn")
    public ReturnStructure deleteCustomizeColumn(@RequestBody CustomizeColumn customizeColumn) {
        if (customizeColumn == null){
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("删除自定义列头信息成功",customizeColumnService.deleteCustomizeColumn(customizeColumn.getUsername(),customizeColumn.getResourceType()));
    }

    @ResponseBody
    @PostMapping("/updateCustomizeColumn")
    public ReturnStructure updateCustomizeColumn(@RequestBody CustomizeColumn customizeColumn) {
        if (customizeColumn == null){
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("修改自定义列头信息成功",customizeColumnService.updateCustomizeColumn(customizeColumn));
    }

    @ResponseBody
    @GetMapping("/getCustomizeColumn/{username}/{resourceType}")
    public ReturnStructure getCustomizeColumn(@PathVariable("username") String username,@PathVariable("resourceType") String resourceType) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(resourceType)){
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("获取自定义列头信息成功",customizeColumnService.getCustomizeColumn(username,resourceType));
    }

    /**
     * 设置默认的资源类别显示字段
     * @param resourceType 资源类别英文名
     * @return 生成结果
     */
    @ResponseBody
    @PostMapping("/generateDefaultColumn")
    public ReturnStructure generateDefaultColumn(@RequestParam("resourceType") String resourceType){


        if (StringUtils.isBlank(resourceType)){
            return ReturnStructure.NOT_FOUND();
        }

        boolean generateSuccess = customizeColumnService.generateDefaultColumn(resourceType);

        return ReturnStructure.SUCCESS(generateSuccess?"自动生成默认列信息成功":"自动生成默认列信息失败",null);

    }

}
